package com.huanhong.hik.socket;

import cn.hutool.core.thread.ThreadUtil;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class SocketIoServer {

    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9999);
        config.setMaxFramePayloadLength(65536*5);
        SocketIOServer server = new SocketIOServer(config);
        // 添加客户端连接监听器
        server.addConnectListener(client -> {
            log.info(client.getRemoteAddress() + " web客户端接入");
            //不知道如何与客户端对应，好的办法是自己去写对应的函数
            client.sendEvent("connected", "hello");
        });

        //监听客户端事件，client_info为事件名称，-自定义事件
        server.addEventListener("client_info", String.class, (client, data, ackRequest) -> {
            //客户端推送advert_info事件时，onData接受数据，这里是string类型的json数据，还可以为Byte[],object其他类型
            String sa = client.getRemoteAddress().toString();
            String clientIp = sa.substring(1, sa.indexOf(":"));//获取客户端连接的ip
            Map params = client.getHandshakeData().getUrlParams();//获取客户端url参数
            System.out.println(clientIp + "：客户端：************" + data);
        });
        server.addEventListener("heartbeat", String.class, (client, data, ackRequest) -> {
            //客户端推送advert_info事件时，onData接受数据，这里是string类型的json数据，还可以为Byte[],object其他类型
            String sa = client.getRemoteAddress().toString();
            String clientIp = sa.substring(1, sa.indexOf(":"));//获取客户端连接的ip
            Map params = client.getHandshakeData().getUrlParams();//获取客户端url参数
            System.out.println(clientIp + "：客户端：************" + data);
            client.sendEvent("heartbeat", "heartbeat");
        });
        server.addEventListener("getCard", String.class, (client, data, ackRequest) -> {
            //客户端推送advert_info事件时，onData接受数据，这里是string类型的json数据，还可以为Byte[],object其他类型
            String sa = client.getRemoteAddress().toString();
            String clientIp = sa.substring(1, sa.indexOf(":"));//获取客户端连接的ip
            Map params = client.getHandshakeData().getUrlParams();//获取客户端url参数
            System.out.println(clientIp + "：客户端：************" + data);
            client.sendEvent("heartbeat", "heartbeat");
        });
        server.addEventListener("setCard", String.class, (client, data, ackRequest) -> {
            //客户端推送advert_info事件时，onData接受数据，这里是string类型的json数据，还可以为Byte[],object其他类型
            String sa = client.getRemoteAddress().toString();
            String clientIp = sa.substring(1, sa.indexOf(":"));//获取客户端连接的ip
            Map params = client.getHandshakeData().getUrlParams();//获取客户端url参数
            System.out.println(clientIp + "：客户端：************" + data);
            client.sendEvent("heartbeat", "heartbeat");
        });
        server.addEventListener("getFace", String.class, (client, data, ackRequest) -> {
            //客户端推送advert_info事件时，onData接受数据，这里是string类型的json数据，还可以为Byte[],object其他类型
            String sa = client.getRemoteAddress().toString();
            String clientIp = sa.substring(1, sa.indexOf(":"));//获取客户端连接的ip
            Map params = client.getHandshakeData().getUrlParams();//获取客户端url参数
            System.out.println(clientIp + "：客户端：************" + data);
            client.sendEvent("heartbeat", "heartbeat");
        });

        //添加客户端断开连接事件
        server.addDisconnectListener(client -> {
            String sa = client.getRemoteAddress().toString();
            String clientIp = sa.substring(1, sa.indexOf(":"));//获取设备ip
            System.out.println(clientIp + "-------------------------" + "客户端已断开连接");
            //给客户端发送消息
            client.sendEvent("advert_info", clientIp + "客户端你好，我是服务端，期待下次和你见面");
        });
        server.start();

        do {
            ThreadUtil.sleep(20000);
            //广播消息
//            server.getBroadcastOperations().sendEvent("borcast", "are you live?");
            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "login", "10.0.0.223", 8000, "admin", "hik12345");
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "getNetwork");
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "setNetwork","10.0.0.145",7200);
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "getAllCard");
//            ThreadUtil.sleep(5000);
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "getCard", "111013");
//            Map<Integer, Integer[]> doorRight = new HashMap<>(8);
//            doorRight.put(0, new Integer[]{1});
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "setCard", "111015", 1, "2010-01-01 00:00:00", "2020-01-01 00:00:00",
//                    1, JSON.toJSONString(doorRight), 0, 124, "测试", "123456");
            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "getFace","111013");
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "setFace","111013","https://img.aiairy.com/IDCard/微信图片_20190408162833.jpg");
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "setupAlarmChan");
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "closeAlarmChan");
//            server.getBroadcastOperations().sendEvent("accessControl", "10.0.0.101", "setPlan");

        } while (true);
//        ThreadUtil.execute(()->{
//            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//            for (; ; ) {
//                server.addEventListener("accessControl", String.class, (client, data, ackRequest) -> {
//
//                    client.sendEvent("accessControl", reader.readLine() + "\n");
//                });
//            }
//        });

    }

}
